package com.Jano.dao.Impl;

import com.Jano.dao.UserDao;
import com.Jano.dto.FriendDto;
import com.Jano.entity.User;
import org.junit.Test;

import java.util.List;
import java.util.Objects;

/**
 * description: UserDaoImpl <br>
 * date: 2022/6/10 4:01 PM <br>
 * author: zoumaoji <br>
 * version: 1.0 <br>
 */
public class UserDaoImpl extends AbstractDaoImpl implements UserDao {
    @Override
    public User login(String userName, String passWord) {
        return queryForObject("select id userId,userName,pwd passWord,phone,singnature singNature,hobbies,address from user where userName=? and pwd =?",User.class,new Object[]{userName,passWord});
    }

    @Override
    public List<User> selectAllUsers() {
        return queryForList("select id userId,userName,pwd passWord,phone,singnature singNature,hobbies,address from user",User.class);
    }

    @Override
    public Boolean isExist(String userName) {
        return !Objects.isNull(queryForObject("select userName from user where userName=?", User.class, new Object[]{userName}));
    }

    @Override
    public void register(String userName, String passWord) {
        update("insert into user (userName,pwd) values(?,?)",new Object[]{userName, passWord});
    }

    @Override
    public List<FriendDto> selectFriendsIdByUserId(Integer userId) {
        return queryForList("select f.friendId,u.userName friendName from friend f join user u on f.friendId = u.id where f.userId=?", FriendDto.class,new Object[]{userId});
    }
}
